package com.shop.zzh.component;

import com.shop.zzh.config.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @program: shop
 * @description: 消息队列生产者
 * @author: zhuzh
 * @create: 2019-11-21 18:04
 **/
@Component
public class SendMessage implements RabbitTemplate.ConfirmCallback{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Autowired
	private Environment env;


	public void SendMessage(String content) {
		CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
		rabbitTemplate.convertAndSend(env.getProperty("log.user.exchange.name"), env.getProperty("log.user.routing.key.name"), content, correlationId);
//		rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话，那回调就是最后设置的内容

	}

	@Override
	public void confirm(CorrelationData correlationData, boolean b, String s) {
		logger.info(" 回调id:" + correlationData);
		if (b) {
			logger.info("消息成功消费");
		} else {
			logger.info("消息消费失败:" + s);
		}
	}
}
